<?php

namespace App\Services;

use App\Models\CoreConfig;
use App\Models\CoreSetting;
use App\Models\SystemConfig;
use Illuminate\Support\Facades\Redis;

class ConfigService extends BaseService
{
    /**
     * SYS配置
     * @param $type
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public static function getSysValueByType($type)
    {
        $saveConfig = Redis::get('legw_sys_config_'.$type);
        if($saveConfig){
            return unserialize($saveConfig);
        }
        $configs = SystemConfig::whereType($type)->first();
        $config = $configs->config;
        if(!$config['1_level_name']){
            $config['1_level_name'] = '一级';
        }
        if(!$config['2_level_name']){
            $config['2_level_name'] = '二级';
        }
        if(!$config['3_level_name']) {
            $config['3_level_name'] = '三级';
        }
        Redis::setex('legw_sys_config_'.$type, 12*3600, serialize($config));
        return $config;
    }

    /**
     * CORE配置
     * @param $type
     * @return mixed
     */
    public static function getCoreValueByType($type)
    {
        $value = Redis::get('legw_core_config_'.$type);
        if(!$value){
            $configs = CoreSetting::where('key', $type)->first();
            $value = $configs->value;
            Redis::setex('legw_core_config_'.$type, 12*3600, $value);
        }
        return unserialize($value);
    }
}